package cn.codewen.servlet;

import cn.codewen.pojo.User;
import cn.codewen.service.UserService;
import cn.codewen.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {


    //0. 创建userService对象
    // private AdminService adminService=new AdminServiceImpl();
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 设置字符集
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        //2. 接收参数
        String m = request.getParameter("m");
        //3. 判断
   /*     if (m.equals("list")) {
            list(request, response);
        } else if (m.equals("add")) {
            add(request, response);
        } else if (m.equals("edit")) {
            edit(request, response);
        } else if (m.equals("del")) {
            del(request, response);
        }*/

        switch (m) {
            case "list":
                list(request, response);
                break;
            case "add":
                add(request, response);
                break;
            case "edit":
                edit(request, response);
                break;
            case "del":
                del(request, response);
                break;
            case "login":
                login(request,response);
                break;
            case "reg":
                reg(request,response);
                break;
            case "logout":
                logout(request,response);
                break;
        }

    }

    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {

       //删除user的Session
        request.getSession().removeAttribute("user");

        //跳转到登录页面
        response.sendRedirect("/user?m=login");
    }

    private void reg(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {


        //0. 判断提交方式是GET还是POST
        if (request.getMethod().equals("POST")) {
            //POST方式处理数据
            //1. 接收参数
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String sex = request.getParameter("sex");

            //2. 处理数据
            //2.0 创建User对象并赋值
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setSex(Integer.parseInt(sex));
            //2.1 创建adminService对象
            //2.2 调用对应的regUser方法增加一个账号
            Boolean rs = userService.regUser(user);
            //3. 转发或跳转
            //3.1 转发数据
            //3.2 跳转
            if (rs){
                //3.2.1 设置Session
                //request.getSession().setAttribute("user",user);
                //3.2.2 成功跳转到后台首页
                response.sendRedirect("/login.jsp");
            }else {
                request.setAttribute("errMsg","注册失败");
                request.getRequestDispatcher("/reg.jsp").forward(request,response);
            }

        } else {
            //GET方式做展示

            //1. 接收参数
            //2. 处理数据
            //3. 转发或跳转
            request.getRequestDispatcher("/reg.jsp").forward(request,response);

        }


    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //0. 判断提交方式是GET还是POST
        if (request.getMethod().equals("POST")) {
            //POST方式处理数据
            //1. 接收参数
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            //2. 处理数据
            //2.1 创建adminService对象
            //2.2 调用对应的checkAdmin方法验证账号密码
            User user = userService.checkUser(username, password);
            //3. 转发或跳转
            //3.1 转发数据
            //3.2 跳转
            if (user!=null){
                //3.2.1 设置Session
                request.getSession().setAttribute("user",user);
                //3.2.2 成功跳转到后台首页
                response.sendRedirect("/index.jsp");
            }else {
                request.setAttribute("errMsg","账号或密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }

        } else {
            //GET方式做展示

            //1. 接收参数
            //2. 处理数据
            //3. 转发或跳转
            request.getRequestDispatcher("/login.jsp").forward(request,response);

        }


    }

    private void del(HttpServletRequest request, HttpServletResponse response) {
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) {
    }

    private void add(HttpServletRequest request, HttpServletResponse response) {
    }

    private void list(HttpServletRequest request, HttpServletResponse response) {
    }


}
